iT邦幫忙

2024 iThome 鐵人賽

DAY 18
0

Current Sprint: 3. 遊戲基本流程完成
repo: https://github.com/side-project-at-SPT/ithome-ironman-2024-san-juan
swagger docs: https://side-project-at-spt.github.io/ithome-ironman-2024-san-juan/

前情提要

  • 城市建築卡片資料完成

sprint 3 遊戲基本流程完成

  • 建築卡片資料
    • 工廠建築卡片資料
    • 城市建築卡片資料
  • 建築卡片功能實作
  • 選職業:開始職業階段
  • 執行 礦工 階段行動
  • 執行 議員 階段行動
  • 執行 建築師 階段行動
  • 執行 製造商 階段行動
  • 執行 貿易商 階段行動
  • 行動結束,更換目前玩家
  • 回合開始
  • 遊戲結束

回顧一下目前選職業的 controller action

  def play
    @game = Game.find(params[:id])
    @game.play

    return render status: :unprocessable_entity, json: { error: @game.errors.full_messages } if @game.errors.any?

    @message = "你選擇了礦工"
  end

根據 Day 15 定義的 api format

  • 選擇職業 POST /api/v1/games/{id}/roles/{role}

下面來更新他

選職業:開始職業階段

routes

# config/routes.rb

Rails.application.routes.draw do
# ...

  namespace :api do
    namespace :v1 do
      resources :games, only: [ :index, :create ] do
        member do
          post :play
          scope :roles do
            post ":role", action: :assign, as: :assign_role
          end
        end
      end
    end
  end
end

controller

# app/controllers/api/v1/game_controller.rb

class Api::V1::GamesController < ApplicationController
# ...
  
  def assign
    @game = Game.find(params[:id])
    @current_player ||= "dummy player"
    @game.assign_role(role: params[:role], player: @current_player)
    if @game.errors.any?
      return render status: :unprocessable_entity, json: {
        error: @game.errors.full_messages
      }
    end

    @message = "你選擇了: #{params[:role]}"
    render json: { message: @message }
  end

# ...
end

model

# app/models/game.rb

class Game < ApplicationRecord
# ...
  
  def assign_role(role:, player:)
    return errors.add(:status, "can't be blank") unless status_playing?

    roles = {
      "builder" => "建築師",
      "producer" => "製造商",
      "trader" => "貿易商",
      "prospector" => "礦工",
      "councillor" => "議員"
    }
    return errors.add(:role, "#{role} is invalid") unless (roles.keys + roles.values).include?(role)

    self
  end

# ...
end

測試看看

  1. 建立遊戲
# rails s

curl localhost:3000/api/v1/games -d '' | jq
# {
#   "id": 6,
#   "status": "playing",
#   ...
  1. 選擇職業:戰士
curl localhost:3000/api/v1/games/6/roles/戰士 -d '' | jq
# {
#   "error": [
#     "Role 戰士 is invalid"
#   ]
# }
  1. 選擇職業:礦工
curl localhost:3000/api/v1/games/6/roles/礦工 -d '' | jq
# {
#   "message": "你選擇了: 礦工"
# }

小結

明天把選職業的方法寫完

TODO

  • 建築卡片資料
    • 工廠建築卡片資料
    • 城市建築卡片資料
  • 建築卡片功能實作
  • 🚧 選職業:開始職業階段
  • 執行 礦工 階段行動
  • 執行 議員 階段行動
  • 執行 建築 階段行動
  • 執行 生產 階段行動
  • 執行 交易 階段行動
  • 行動結束,更換目前玩家
  • 回合開始
  • 遊戲結束

明天要做什麼

  • 🚧 選職業:開始職業階段
  • 執行 礦工 階段行動
  • 執行 議員 階段行動

以上不代表明天會做,如有雷同純屬巧合


工商服務

SPT (Side Project Taiwan) 的宗旨是藉由Side Project開發來成就自我,透過持續學習和合作,共同推動技術和專業的發展。我們相信每一個參與者,無論是什麼專業,都能在這個社群中找到屬於自己的成長空間。

歡迎所有對Side Project開發有興趣的人加入我們,可以是有點子來找夥伴,也可以是來尋找有興趣的Side Project加入,邀請大家一同打造一個充滿活力且有意義的技術社群!

Discord頻道連結: https://sideproj.tw/dc


上一篇
Day 17 - 實作城市建築卡片的資料
下一篇
Day 19 - 選職業(2/n)
系列文
透過實作網頁遊戲練習網站工程師的基本素養,以 San Juan(聖胡安) 為例。30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言